﻿@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    List<WeChatParking.Entity.Enterprise> list = ViewBag.EnterpriseList;
}
<div class="">
    <div class="ibox-content">
        <div class="jqGrid_wrapper">
            <div class="col-sm-12 jqGrid-toolbar">
                <form class="form-inline search-form" id="search-form">
                    <div class="form-group">
                        <label for="search_card_no">停车场</label>
                        <select class="form-control  sm" name="Enterprise_Code" style="width:120px;">
                            @foreach (var item in list)
                            {
                                  <option value="@item.Enterprise_Code">@item.Enterprise_Name</option>
                            }
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="search_card_no">车牌号</label>
                        <input type="text" class="form-control" id="search_card_no" style="width: 120px;" name="carBrand">
                    </div>
                    <div class="form-group">
                        <label for="search_pay_status">车辆状态</label>
                        <select class="form-control  sm" id="search_status" name="status">
                            <option value="101">全部</option>
                            <option value="1">在场</option>
                            <option value="2">出场</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="search_paytime_begin">进场时间</label>
                    </div>
                    <div class="form-group">
                        <div>
                            <input type="text" class="form-control laydate-icon layer-date" id="search_paytime_begin" name="time_begin" value="@DateTime.Today.AddDays(-6).ToString("yyyy-MM-dd")">
                            <input type="text" class="form-control laydate-icon layer-date" id="search_paytime_end" name="time_end" value="@DateTime.Today.ToString("yyyy-MM-dd")">
                        </div>

                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                </form>

                <div class="btn-group " role="group">
                    <button class="btn btn-primary" id="btn-export">
                        <i class="glyphicon glyphicon-download-alt"></i>导出
                    </button>
                </div>
            </div>
            <table id="grid"></table>
            <div id="grid_pager"></div>
        </div>
    </div>
</div>

@section BodyFooter{
    <script>
        $(document).ready(function () {

            $("#grid").jqGrid({
                url: '/Admin/CarRecordQuery',
                multiselect: false,
                colNames: ["车牌号", "车型", "车场代码", "车场名", "状态", "进场时间", "出场时间", "创建时间"],
                colModel: [{
                    name: "carBrand",
                    index: "carBrand",
                    key: true,
                },
                {
                    name: "retain1",
                    index: "retain1",
                    formatter: formatRetain1,
                    width: 70
                },
                {
                    name: "Enterprise_Code",
                    index: "Enterprise_Code",
                    width: 80
                },
                {
                    name: "Enterprise_Name",
                    index: "Enterprise_Name",
                    width: 80
                },
                {
                    name: "status",
                    index: "status",
                    formatter: formatPayStatus,
                    width: 70
                },
                 {
                     name: "inTime",
                     index: "inTime",
                     width: 120,
                     formatter: 'date',
                     formatoptions: { newformat: 'Y-m-d H:i:s' }
                 },
                  {
                      name: "outTime",
                      index: "outTime",
                      width: 120,
                      formatter: 'date',
                      formatoptions: { newformat: 'Y-m-d H:i:s' }
                  },
                {
                    name: "create_time",
                    index: "create_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                }],
                caption: "进出记录",
                footerrow: true,
                userDataOnFooter: true
            });

            $("#grid").adaptGridSizeInWindow();
        });
        $('#search-form').submit(function () {
            var data = $(this).serializeArray();
            var postData = {};
            $.each(data, function (i, n) {
                postData[n.name] = n.value;
            });
            $("#grid").jqGrid('setGridParam', {
                postData: postData, //发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入


            $('#btn-export').data('export-query', postData);

            return false;
        });
        $('#btn-export').click(function () {

            var postData = $('#btn-export').data('export-query');
            if (!postData) {
                toastr.error('请先执行查询');
                return;
            }

            $.postJsonAjax('/Admin/DownloadCarRecor', postData, function (rsp) {
                if (rsp.Code == 1) {
                    var el = $('<a id="download-file" style="display:none"></a>').appendTo(document.body);
                    el[0].href = rsp.Message;
                    el[0].click();
                    el.remove();
                } else {
                    toastr.error(rsp.Message);
                }
            });
        });

        var formatPayStatus = function (cellvalue, options, row) {
            if (cellvalue == 1) {
                return '<span class="label label-danger">在场</span>';
            }
            if (cellvalue == 2) {
                return '<span class="label label-success">出场</span>';
            }
            return '';
        }
        var formatRetain1 = function (cellvalue, options, row) {
            if (cellvalue == 1) {
                return '小车';
            }
            if (cellvalue == 2) {
                return '货车';
            }
            return '';
        }
    </script>

    <script>
        var start = {
            elem: '#search_paytime_begin',
            format: 'YYYY-MM-DD',
            max: laydate.now(), //最大日期
            istime: false,
            istoday: true,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#search_paytime_end',
            format: 'YYYY-MM-DD',
            max: laydate.now(),
            istime: false,
            istoday: true,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
    </script>

}
